var changeArray =  new Array();
var isChange = false;


function loaddata(){
	//var allFlag = $("#allFlag").val();
	//var pno = $("#pagenum").val();
	var emp_id = $("#emp_id").val();
	var classtype=$("#classtype").val();
	var ope_id=$("#ope_id_Lcm").val();
	var month= $("#month").val();
	var scope= $("#scope").val();
	
	
	var url = pms_url + "lcm/performance/getListCompAtibility";
	var params = {
			emp_id:emp_id,
			ope_id:ope_id,
			month:month,
			classtype:classtype,
			scope:scope
			//pno:pno,
			//allFlag:allFlag
			};
	var obj = load(url,params);
	var compAtibilityArray = obj.compAtibility;
	$("#performanceMaintainMonlist").find("tbody").html("");
	$.each(compAtibilityArray, function(index, p) {
	var tr ="<tr role='row'>"+
	"<td align='center'>"+(index+1)+"</td>"+
	"<td align='center'>"+p.label+"</td>"+
	"<td align='center'>"+p.emp_id+"</td>"+
	"<td align='center'>"+p.emp_name+"</td>"+
	"<td align='center'>"+((p.compatibility == null) ? "":p.compatibility)+"</td>" +
	"<td style = \"display:none\">"+p.category+"</td></tr>";
	//alert(tr);
	$("#performanceMaintainMonlist").find("tbody").append(tr);
 });
}




function save(){
	
	if(isChange){
		
		var a = new Array();
		var month=$("#month").val();
		var table = document.getElementById("performanceMaintainMonlist");
		changeArray.sort();
		var newArray = new Array();
		for(var i = 0; i < changeArray.length; i++){
			if(i > 0){
				if(changeArray[i] != changeArray[i-1])
					newArray.push(changeArray[i]);
			}else{
				newArray.push(changeArray[i]);
			}
		}
		
		 
		$('table tr').each(function(i){
			 
			if($.inArray(i, newArray) != -1){
				var tablelist = new Object();
				tablelist.month = month;
				tablelist.emp_id = $(this).children("td:eq(2)").text();
				tablelist.compatibility = $(this).children("td:eq(4)").text();
				a.push(tablelist);
				
			}
			
		});
		
		var obj =JSON.stringify(a);
		
		var aurl = pms_url + "lcm/performance/updateCompatibilityByMon";
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : {param:obj},
	        dataType : "json", 
	        async : false,  //同步
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			if(data.result == "0"){
		   				alert("保存成功");
		   				loaddata();
		   			}else{
		   				alert("error!");
		   			}
				}else {
					alert("error!");
				}
	        }  
	    });
		
		
		
		
		//alert(table.rows[0].cells[2].innerHTML);
		
		/*$('table tr').each(function(trindex,tritem){
        $(tritem).find("td").each(function(tdindex,tditem){
       	 
       	 if(tdindex = $(tritem).find("td").length-1){
       		 a.data.add(tablelist);
       	 }
        });
    });
	*/
		
	
	}else{
		alert("内容无改动，无需保存！");
		return;
	}
	
	isChange = false;
	
}







$(function () { 
	
	loaddata();
	$("#btnSearch").click(function(){
		loaddata();
	});
	
	$("#ope_id_Lcm").change(function(){
		loaddata();
	});
	
	
	
	
	$("#btnReset").on('click', function () {
		$("#searchform input").val("");
		$("#ope_id_Lcm").val("");
		$("#emp_id").val("");
		$("#scope").val("");
	});	
	
	
	
	$("#btnSave").click(function(){
		save();
	});
	
	
	
	
	/***********************/
	
	$(document).on("dblclick","td",function(){

		if($(this).children("input").length > 0) 
			   return false;

		var td = $(this);

		var trindex = td.parents("tr").index() + 1;
		var ope_id= td.parent().children("td:eq(1)").html();
		var dept= td.parent().children("td:eq(5)").html();
		
		changeArray.push(trindex);

		var tdindex = td.parents("tr").find("td").index($(this));
		if(tdindex == 4){
			var td = $(this);  
		    //保存初始值  
		    var tdText = $(this).text();  
		    //给td设置宽度和给input设置宽度并赋值  
		    var input = $("<input type='text' />");
		    td.text("");
		    input 
			   .width(td.width()-200)                   //设置文本框宽度与td相同 
			   .height(td.height()) 
			   .val(tdText) 
			   .appendTo(td)                         //把创建的文本框插入到tdObj子节点的最后
			   .trigger("focus")                        //用trigger方法触发事件 
			   .trigger("select"); 
		    
		    //失去焦点的时候重新赋值  
		    var inputDom = $(this).find("input");  
		    inputDom.blur(function(){  
		    		
		    	var error = false;
		    	
		    	var newText = $(this).val();  
		    	
		    	if (isNaN(newText)){   //用于检查其参数是否是非数字值  
		            alert("必须输入数字！");
		            td.html(tdText); 
		            return;
		        }
		    	
		    	if(newText>15 && (dept == "24" || dept == "25")) {
					alert("制造部配合度最高分是15,请确认！");
					error = true;
				}else if(newText>30 && (dept == "38" || dept == "39")){
					alert("工程部配合度最高分是30,请确认！");
					error = true;
				}
		    	
		       
		       
		        $(this).remove();  
		        
		        if(error){
		        	td.text(tdText);
		        }else{
		        	td.text(newText);
		        }
		        
		        if(newText != tdText){
			    	isChange = true;
			    }
		        
		    });
		}



		});
	
	
	
	
	
	
	
	
  });